/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年5月13日         luwei
 */

package com.coocaa.x.framework.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ThirdMac {
    public static final String PATH = "/sys/class/net/";
    public static final String WIFI = "wlan0/";
    public static final String ETHER = "eth0/";
    public static final String FILE = "address";

    public static String getEtherMac() {
        String path = PATH + ETHER + FILE;
        return getMac(path);
    }

    private static String getMac(String path) {
        File file = new File(path);
        String mac = "";
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        if (file.exists()) {
            try {
                is = new FileInputStream(file);
                isr = new InputStreamReader(is);// InputStreamReader 是字节流通向字符流的桥梁,
                br = new BufferedReader(isr);// 从字符输入流中读取文件中的内容,封装了一个new InputStreamReader的对象
                mac = br.readLine();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        return mac;
    }

    public static String getWifiMac() {
        String path = PATH + WIFI + FILE;
        return getMac(path);
    }

    public static void main(String[] args) {
        System.out.println(getEtherMac());
        System.out.println(getWifiMac());
    }
}
